package shank;

import java.util.Random;

import net.slashie.libjcsi.CharKey;
import net.slashie.libjcsi.ConsoleSystemInterface;
import net.slashie.libjcsi.wswing.WSwingConsoleInterface;

public class GameConsole {
	private char[][] map;
	Random sprw = new Random();
	Random spcl = new Random();
	private int charRow = sprw.nextInt(15);
	private int charCol = spcl.nextInt(60);
	private ConsoleSystemInterface csi;
	private int screenRow = 0;
	private int screenRowMax;
	private int screenCol;
	private int screenColMax;
	private boolean alive = true;
	GameCharacter toon = new GameCharacter();
	
	public char[][] getMap() {
		return map;
	}

	public void setMap(char[][] map) {
		this.map = map;
	}

	public GameConsole()
	{
    	csi = new WSwingConsoleInterface("libjcsi example - Santiago Zapata", false);
    	csi.cls();
		createMap();
		randomTrees();
    	drawMap(getMap());
	}
	
	public void createMap()
	{
		map = new char[200][200];
		for (int i=0;i<200;i++)
		{
			for (int j=0;j<200;j++)
			{
				map[i][j] = '.';
			}
		}
		
		map[charRow][charCol] = toon.getSprite();
		toon.setRowCol(charRow, charCol);

	}
	
	public void randomTrees()
	{
		Random rw = new Random();
		Random cl = new Random();
		
		for (int i=0;i<1000;i++)
		{
			map[rw.nextInt(200)][cl.nextInt(200)] = 'T';
		}
	}
	
	public void moveToon(CharKey k)
	{
		int newCharRow = charRow;
		int newCharCol = charCol;
		if (k.code == CharKey.j && charRow > 0)
		{
			newCharRow = charRow-1;
			newCharCol = charCol;
		}
		if (k.code == CharKey.k && charRow < 199)
		{
			newCharRow = charRow+1;
			newCharCol = charCol;
		}
		if (k.code == CharKey.h && charCol > 0)
		{
			newCharRow = charRow;
			newCharCol = charCol-1;
		}
		if (k.code == CharKey.l && charCol < 200)
		{
			newCharRow = charRow;
			newCharCol = charCol+1;
		}
		if (map[newCharRow][newCharCol] == 'T')
		{
			return;
		}
		map[charRow][charCol] = '.';
		map[newCharRow][newCharCol] = toon.getSprite();
		charRow = newCharRow;
		charCol = newCharCol;
	}
	
    public void updateUI()
    {
    	moveToon(csi.inkey());
    	csi.cls();
    	drawMap(getMap());
    	csi.refresh();
    }
    
    public void drawMap(char[][] board)
    {
    	screenRowMax = screenRow+15;
    	screenColMax = screenCol+60;
    	int y = 1;
    	for (int i=0;i<screenRowMax;i++)
    	{
    		int x = 1;
    		for (int j=0;j<screenColMax;j++)
    		{
    			if (board[i][j] == 'T')
    			{
    			csi.print(x,y,board[i][j],ConsoleSystemInterface.GREEN);
    			}
    			else
    			{
    			csi.print(x,y,board[i][j],ConsoleSystemInterface.WHITE);	
    			}
    			x++;
    		}
    		y++;
    	}
    }
}
